﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

using IsmlToolsWPF.Dal;
using Telerik.Windows.Controls;
using IsmlToolsWPF.Localization;
using Telerik.Windows;
using System.Linq.Expressions;
using Telerik.Windows.Data;
using System.ComponentModel;
using Telerik.Windows.Controls.Data.DataPager;

namespace IsmlToolsWPF
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        //private FilterDescriptor<MyBusinessObject> genericFilterDescriptor = new FilterDescriptor<MyBusinessObject>();

        public MainWindow()
        {
            InitializeComponent();
            LoadDB();
        }

        private void LoadDB()
        {
            LocalizationManager.Manager = new CustomLocalizationManager();

            IsmlEntities db = new IsmlEntities();

            //SELECT dbo.Season.Year AS 年份, dbo.Period.Name AS 阶段, dbo.Round.Name AS 场次, 
            //dbo.Match.Name AS 比赛, dbo.Match.ID AS 比赛ID, 
            //dbo.SeasonName.SimplifiedChinese AS 名字, dbo.Vote.Value AS 票数
            //FROM dbo.Match INNER JOIN
            //dbo.Vote ON dbo.Match.ID = dbo.Vote.MatchID INNER JOIN
            //dbo.Round ON dbo.Match.RoundID = dbo.Round.ID INNER JOIN
            //dbo.Period ON dbo.Round.PeriodID = dbo.Period.ID INNER JOIN
            //dbo.Season ON dbo.Period.SeasonID = dbo.Season.ID INNER JOIN
            //dbo.SeasonName ON dbo.SeasonName.SeasonID = dbo.Season.ID AND 
            //dbo.SeasonName.NameCode = dbo.Vote.SeasonNameCode

            var query = from v in db.Votes
                        join m in db.Matches on v.MatchID equals m.ID
                        join r in db.Rounds on m.RoundID equals r.ID
                        join p in db.Periods on r.PeriodID equals p.ID
                        join s in db.Seasons on p.SeasonID equals s.ID
                        from sn in db.SeasonNames
                        where (s.ID == sn.SeasonID) && (v.SeasonNameCode == sn.NameCode)

                        select new
                        {
                            年份 = s.Year,
                            阶段 = p.Name,
                            场次 = r.Name,
                            比赛 = m.Name,
                            比赛ID = m.ID,
                            名字 = sn.SimplifiedChinese,
                            票数 = v.Value
                        };

            MatchesRadGridView.ItemsSource = query.ToList();
        }
    }
}
